package com.bflow.impl;

import com.bflow.api.IBflowCallbackRecordService;
import com.bflow.api.bo.BflowCallbackRecordBo;
import com.bflow.commons.exception.BizException;
import com.bflow.commons.service.BaseService;
import com.bflow.dao.BflowCallbackRecordDao;
import com.bflow.dao.po.BflowCallbackRecordPo;
import com.bflow.util.AssertUtil;
import com.bflow.util.BeanMapper;
import com.bflow.util.SysSeqUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Name BflowCallbackRecord 流程回调记录-实现业务接口
 * @Author haowen
 * @Date 2019-01-10
 */
@Service("bflowCallbackRecordService")
public class BflowCallbackRecordServiceImpl extends BaseService<BflowCallbackRecordPo, BflowCallbackRecordBo> implements IBflowCallbackRecordService<BflowCallbackRecordPo, BflowCallbackRecordBo> {

	private static final Logger logger = LoggerFactory.getLogger(BflowCallbackRecordServiceImpl.class);

	@Autowired
	private BflowCallbackRecordDao dao;

	@Override
	protected BflowCallbackRecordDao getDao() {
		return dao;
	}

	private void validate(BflowCallbackRecordBo bo){
		AssertUtil.isNotNull(bo, "提交不能为空");
		AssertUtil.isNotNull(bo.getInstanceId(), "流程实例ID 不能为空");
		AssertUtil.isNotNull(bo.getTitle(), "标题 不能为空");
		AssertUtil.isNotNull(bo.getBusiKey(), "业务KEY 不能为空");
		AssertUtil.isNotNull(bo.getBusiId(), "业务ID 不能为空");
		AssertUtil.isNotNull(bo.getParamVals(), "参数值 不能为空");
	}

    @Transactional
	@Override
	public Long add(BflowCallbackRecordBo bo) {
		validate(bo); //校验
		Long id = SysSeqUtil.generateId();
		bo.setId(id);
		BflowCallbackRecordPo po = BeanMapper.map(bo, BflowCallbackRecordPo.class);
		if (dao.insert(po) <= 0) {
			throw new BizException("新增失败");
		}
		return id;
	}

}
